
import sqlite3

class qudb:
    def __init__(self):
        self.db=sqlite3.connect('questions.db')
        self.cu=self.db.cursor()
        self.insert_count=0

    def isQuestionExists(self,detail):
        r = self.cu.execute("select count(*) from question where detail=?",[detail])
        count = int(r.fetchall()[0][0])

        return count==1

    def InsertNewQuestion(self,qid,detail):
        last_id=0
        if not self.isQuestionExists(detail):
            r=self.cu.execute('insert into question (qid,detail) values (?,?)',[qid,detail])
            self.db.commit()

            r=self.cu.execute('select last_insert_rowid()')
            last_id=int(r.fetchall()[0][0])
            self.insert_count=self.insert_count+1

        return last_id

    def InsertNewOption(self,qid,option_detail):
        self.cu.execute('insert into option (qid,detail) values (?,?)',[qid,option_detail])
        self.db.commit()

    def GetAnswerByQuestion(self,detail):
        r=self.cu.execute('select detail from answer where qid in (select id from question where question.detail=?) ',[detail])

        answer=r.fetchall()[0][0]
        return answer


